<?php require_once(dirname(__FILE__).'/inc/config.inc.php');IsModelPriv('salesman');
	$id = empty($_POST['id']) ? '' :$_POST['id'];
    $order_id = empty($_POST['order_id']) ? '' :$_POST['order_id'];
    $state = empty($_POST['state']) ? '' :$_POST['state'];
    if(!empty($id) && !empty($state)){
        $time = time();
		if($state == 1){
			$order = $dosql->GetOne("SELECT * FROM `#@__goods_order` WHERE id={$order_id}");
			$refund = $dosql->GetOne("SELECT * FROM `#@__refund` WHERE id={$id}");
			if(!empty($order)){
				require_once(dirname(__FILE__).'/inc/wxRefund.php');
				$wxRefund = new wxRefund();
				$refundnum = mt_rand(10,100).time().mt_rand(10,1000);
				$result = $wxRefund->SubmitJsApiWxRefund($order['transaction_id'],$order['ordernum'],$refundnum,$order['money'],$order['money']);
				// print_r($result);exit;
				if($result['return_code'] == 'SUCCESS'){
	                if($result['result_code'] == 'SUCCESS'){
	                    $dosql->ExecNoneQuery("update dyspw_goods_order set state = 6 where id = {$order['id']}");
	                    $dosql->ExecNoneQuery("update dyspw_refund set state = 1 , handle_time={$time} where refundnum = {$refund['refundnum']}");
	                    $result['status'] = 'y';
	                    $result['info'] = '退款发起成功，微信将在一个工作日内退款至用户账户';
	                    exit(json_encode($result));
	                }elseif ($result['result_code'] == 'FAIL') {
	                    $result['status'] = 'n';
	                    $result['info'] = $result['err_code_des'];
	                    exit(json_encode($result));
	                }
	            }else{
	                $result['status'] = 'n';
	                $result['info'] = '退款请求失败，请重新提交';
	                exit(json_encode($result));
	            }
			}else{
				$result['states'] = 'n';
		        $result['info'] = '未查询到该订单信息';
		        exit(json_encode($result));
			}
		}else{
			$refund = $dosql->ExecNoneQuery("UPDATE dyspw_refund SET state = 2, handle_time={$time} WHERE `id`={$id}");
	        $dosql->ExecNoneQuery("UPDATE dyspw_goods_order SET state = 7 WHERE `id`={$order_id}");
			if($refund){
				$result['states'] = 'y';
		        $result['info'] = '退款驳回，即将刷新';
		        exit(json_encode($result));
			}else{
				$result['states'] = 'n';
		        $result['info'] = '驳回失败请重新提交';
		        exit(json_encode($result));
			}
		}

    }
 ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>退款管理</title>
<link href="templates/style/admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="templates/js/jquery.min.js"></script>
<script type="text/javascript" src="templates/js/forms.func.js"></script>
</head>
<body>
<div class="topToolbar"> <span class="title">退款管理</span> <a href="javascript:location.reload();" class="reload">刷新</a></div>
<form name="form" id="form" method="post" action="member_save.php">
	<table width="100%" border="0" cellpadding="0" cellspacing="0" class="dataTable">
		<tr align="left" class="head" style="text-align: center;">
			<td width="2%" height="36" class="firstCol"><input type="checkbox" name="checkid" id="checkid" onclick="CheckAll(this.checked);"></td>
			<td width="3%">ID</td>
			<td width="10%">用户名称</td>
			<td width="10%">商户名称</td>
			<td width="10%">商品名称</td>
			<td width="5%">商品金额</td>
			<td width="5%">银行名称</td>
			<td width="10%">银行卡号</td>
			<td width="5%">开户行地址</td>
			<td width="5%">真实姓名</td>
			<td width="5%">联系电话</td>
			<td width="10%">退款详情</td>
			<td width="5%">申请时间</td>
			<td width="5%">审核时间</td>
			<td width="5%" class="endCol" style="text-align: center;">状态</td>
		</tr>
		<?php
		$dopage->GetPage("SELECT * FROM `#@__refund` ");

		while($row = $dosql->GetArray())
		{
			$goods = $dosql->GetOne("SELECT title FROM `#@__goods` WHERE id={$row['goods_id']}");
			$shop = $dosql->GetOne("SELECT username FROM `#@__member` WHERE id={$row['shop_id']}");
			$user = $dosql->GetOne("SELECT username FROM `#@__member` WHERE id={$row['uid']}");
			$order = $dosql->GetOne("SELECT `time` FROM `#@__goods_order` WHERE id={$row['order_id']}");
			if(!empty($row['bank_id']) || $row['bank_id'] != 0){
				$bank = $dosql->GetOne("SELECT * FROM `#@__mem_bank` WHERE id={$row['bank_id']}");
				$row['bank_add'] = $bank['bank_add'];
				$row['bank_name'] = $bank['bank_name'];
				$row['username'] = $bank['username'];
				$row['bank_num'] = $bank['bank_num'];
				$row['bank_mobile'] = $bank['bank_mobile'];
			}
			$row['title'] = empty($goods['title']) ? '' : $goods['title'];
			$row['username'] = $user['username'];
			$row['shopname'] = $shop['username'];
			$row['order_time'] = $order['time'];
		?>
		<tr align="left" class="dataTr" style="text-align: center;">
			<td height="60" class="firstCol"><input type="checkbox" name="checkid[]" id="checkid[]" value="<?php echo $row['id']; ?>" /></td>
			<td><?php echo $row['id']; ?></td>
            <td><?php echo $row['username']; ?></td>
            <td><?php echo $row['shopname']; ?></td>
            <td><?php echo $row['title']; ?></td>
			<td><?php echo $row['money']; ?></td>
			<td><?php if(!empty($row['bank_name'])){echo $row['bank_name'];} ?></td>
			<td><?php if(!empty($row['bank_num'])){echo $row['bank_num'];} ?></td>
			<td><?php if(!empty($row['bank_add'])){echo $row['bank_add'];} ?></td>
			<td><?php if(!empty($row['username'])){echo $row['username'];} ?></td>
            <td><?php if(!empty($row['bank_mobile'])){echo $row['bank_mobile'];} ?></td>
            <td><?php echo $row['reason']; ?></td>
			<td><?php echo date('Y-m-d',$row['time']); ?></td>
			<td><?php if(!empty($row['handle_time']))echo date('Y-m-d',$row['handle_time']); ?></td>
            <td style="text-align: center;">
				<?php if($row['state'] == 1){ ?>
					<div style='font-style: normal;display: inline-block;width: 50px;line-height: 30px;background: #1b7e00c9;color: #fff;border-radius: 5px;font-size: 12px;'>已退款</div>
				<?php }elseif($row['state'] == 2){ ?>
					<div style='font-style:normal; display: inline-block;width: 50px;line-height: 30px;background: #d92424c9;color: #fff;border-radius: 5px;font-size: 12px;'>已驳回</div>
				<?php }else{ ?>
					<a href="#" onclick="onstate($(this));" style="height:30px;width:80px;text-align:center;  font-style:normal; display: inline-block;line-height: 30px;background: #509ee1;color: #fff;border-radius: 5px;font-size: 12px;">审核中</a>
                    <a href="#" onclick="state_ajax(<?php echo $row['id']; ?>,1,<?php echo $row['order_id'] ?>);" class="state_btn" style="margin-right:0px;height:30px;width:80px;text-align:center;  font-style:normal; display:none;line-height: 30px;background: #1b7e00c9;color: #fff;border-radius: 5px;font-size: 12px;">退款</a>
                    <a href="#" onclick="state_ajax(<?php echo $row['id']; ?>,2,<?php echo $row['order_id'] ?>);" class="state_btn" style="margin-right:0px;height:30px;width:80px;text-align:center;  font-style:normal; display:none;line-height: 30px;background: #d92424c9;color: #fff;border-radius: 5px;font-size: 12px;">驳回</a>
				<?php } ?>
            </td>
			<script type="text/javascript">
				function onstate(obj){
					obj.parent().find('.state_btn').css("display",'inline-block');
				}
				function state_ajax(id,state,order_id){
					$.ajax({
						url: '/xiaochengxu/admin/refund.php',
						type: "post",
						data: {'id':id,'state':state,'order_id':order_id} ,
						dataType: 'json',
						success:function(res){
							console.log(res.states);
							alert(res.info);
							if(res.status == 'y')window.location.reload();
						}
					})
				}
			</script>
		</tr>
		<?php
		}
		?>
	</table>
</form>
<?php

//判断无记录样式
if($dosql->GetTotalRow() == 0)
{
	echo '<div class="dataEmpty">暂时没有相关的记录</div>';
}
?>
<!-- <div class="bottomToolbar"><span class="selArea"><span>选择：</span> <a href="javascript:CheckAll(true);">全部</a> - <a href="javascript:CheckAll(false);">无</a> - <a href="javascript:DelAllNone('member_save.php');" onclick="return ConfDelAll(0);">删除</a></span> <a href="member_add.php" class="dataBtn">注册新会员</a> </div> -->
<div class="page"> <?php echo $dopage->GetList(); ?> </div>
<?php

//判断是否启用快捷工具栏
if($cfg_quicktool == 'Y')
{
?>
<!-- <div class="quickToolbar">
	<div class="qiuckWarp">
		<div class="quickArea"><span class="selArea"><span>选择：</span> <a href="javascript:CheckAll(true);">全部</a> - <a href="javascript:CheckAll(false);">无</a> - <a href="javascript:DelAllNone('member_save.php');" onclick="return ConfDelAll(0);">删除</a></span> <a href="member_add.php" class="dataBtn">注册新会员</a> <span class="pageSmall"> <?php echo $dopage->GetList(); ?> </span></div>
		<div class="quickAreaBg"></div>
	</div>
</div> -->
<?php
}
?>
</body>
</html>

<script>
    function changeStatus(status){
        $.post("demo_ajax_gethint.asp",{
            status:status,
            order_id:''
            },function(result){
            $("span").html(result);
        });
    }
</script>
